package service

import (
	"github.com/astaxie/beego/orm"
	"fmt"
	"autoCRUD/models"
)

func AddSpiderItem(item *models.SpiderItem) error{
	o := orm.NewOrm()
	if _,err := o.Insert(item);err != nil{
		return err
	}
	return nil
}

func UpdateSpiderItem(item *models.SpiderItem) error{
	o := orm.NewOrm()
	if _,err := o.Update(item);err != nil{
		return err
	}
	return nil
}

func DeleteSpiderItem(id int) error{
	o := orm.NewOrm()
	if _,err := o.Delete(&models.SpiderItem{Id:id},"id");err != nil{
		return err
	}
	return nil
}

func ListSpiderItem() ([]*models.SpiderItem,error){
	o := orm.NewOrm()
	sql := fmt.Sprintf("select * from spiderItem")
	items := []*models.SpiderItem{}
	_,err := o.Raw(sql).QueryRows(&items)
	if err != nil{
		return nil,err
	}
	return items,err
}

func SpiderItemById(id int) (*models.SpiderItem,error){
	o := orm.NewOrm()
	sql := "select * from spiderItem where id = ?"
	item := &models.SpiderItem{}
	err := o.Raw(sql,id).QueryRow(item)
	if err != nil{
		return nil,err
	}
	return item,nil
}
